package com.shoeshopping.action.joseph;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.infinispan.util.FastCopyHashMap.KeySet;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.shoeshopping.entity.Goods;
import com.shoeshopping.service.joseph.GoodsService;
import com.shoeshopping.utils.joseph.Result;

@Controller("goodsAction")
public class GoodsAction extends ActionSupport implements RequestAware, SessionAware{
	@Resource(name="goodsService")
	private GoodsService goodsService;
	
	private Map<String, Object> request;
	private Map<String, Object> session;
	private List goodsList ;
	private Goods goods;
	private String goodsId;
	private String color;
	private String name;
	private String categoryName;
	
	public String display(){
		goodsList = goodsService.displayAllGoods();
		
		if(goodsList==null){
			session.put("errorMsg", "商店没有商品！");
			return Result.DISPLAY_FAILURE;
		}
		else{
			//将所有商品数据放进request
			request.put("goodses", goodsList);
			return Result.DISPLAY_SUCCESS;
		}
	}
	
	public String detail(){
		if(goodsId==null){
			session.put("errorMsg", "没有指定商品！");
			return Result.DETAILS_FAILURE;
		}
			goods = (Goods)goodsService.displayGoodsById(goodsId);
		if(goods==null){
			session.put("errorMsg", "没有指定商品！");
			return Result.DETAILS_FAILURE;
		}
		return Result.DETAILS_SUCCESS;
	}
	public String detailByColorAndName(){
		System.out.println(name + "---" + color);
		if(name==null&&color==null) {
			System.out.println("没有指定商品");
			return Result.DETAILS_FAILURE;
		}
		goods= goodsService.displayGoodsByNameAndColor(name, color);
		if(goods==null){
			session.put("errorMsg", "没有指定商品！");
			return Result.DETAILS_FAILURE;
		}
		return Result.DETAILS_SUCCESS;
	}
	public String category(){
		List listOfType  = goodsService.displayGoodsByType(categoryName);
		if(listOfType==null || listOfType.size()<=0){
			session.put("errorMsg", "没有指定类型商品或该类型商品数量为零！");
			return Result.CATEGORIES_FAILURE;
		}else{
			request.put("goodsesOfType", listOfType);
		return Result.CATEGORIES_SUCCESS;
		}
	}
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public GoodsService getGoodsService() {
		return goodsService;
	}
	
	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public String getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public List getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List goodsList) {
		this.goodsList = goodsList;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Map<String, Object> getSession() {
		return session;
	}
	
}
